package qnb.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse.SmsSendDetailDTO;
import com.aliyuncs.exceptions.ClientException;

import org.springframework.web.servlet.ModelAndView;
import qnb._common._ModuleKey;
import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.vo.MessageVOFactory;
import qnb._config.Aliyun_sms;
import qnb.entity.*;
import qnb.service.StudentService;
import qnb.service.impl.*;
import weixinkeji.vip.expand.poi.JWEOfficeWebTool;
import weixinkeji.vip.jweb.power.ann.JWPIdentifiter;


@Controller
@RequestMapping("/student")
public class StudentController {

    //跳转页面
    @JWPIdentifiter("student:insert")
    @RequestMapping("/forward/addStudent")
    public ModelAndView forwardAddStu(ModelAndView modelAndView) {
        modelAndView.setViewName("student/addStudent");
        return modelAndView;
    }

    @Autowired
    private StudentService imp;

    @Autowired
    StudentServiceImpl stu;
    @Autowired
    StudentStyleServiceImpl studentStyle;
    @Autowired
    RoomServiceImpl room;
    @Autowired
    SubjectServiceImpl subject;
    @Autowired
    GradeServiceImpl grade;
    @Autowired
    TeacherServiceImpl teacher;
    //处理请求

    /**
     * 填充数据到添加学生页面的学生宿舍下拉列表
     * @return
     */
    @RequestMapping("/roomData")
    @ResponseBody
    public List<Room> roomData() {
        return room.selectAll();

    }
    /**
     * 填充数据到学生页面的学生班级下拉列表
     * @return
     */
    @RequestMapping("/subjectData")
    @ResponseBody
    public List<Subject> subjectData(){
        return subject.selectAll();
    }
    /**
     * 填充班级数据到学生添加页面的选择班级下拉列表当中来
     * @return
     */
    @RequestMapping("/gradeData")
    @ResponseBody
    public List<Grade> gradeData(){
        return grade.selectAllForStu();
    }

    /**
     * 填充状态数据到学生添加页面的状态选择下拉列表当中来
     * @return
     */
    @RequestMapping("/studentStyleData")
    @ResponseBody
    public List<StudentStyle> studentStyleData(){
        return studentStyle.selectAll();
    }

    /**
     * 填充招生老师数据到学生添加页面的状态选择下拉列表当中来
     * @return
     */
    @RequestMapping("/teacherData")
    @ResponseBody
    public List<Teacher> teacherData(){
        return teacher.selectAllTeacher();
    }

    /**
     * 处理添加学生的请求
     * @return
     * @throws ClientException
     */
    @JWPIdentifiter("student:insert")
    @RequestMapping("/addStu")
    @ResponseBody
    public String addStudent(Student student) throws ClientException {
        if(stu.selectOne(student.getStudentId())!=null || stu.selectOneForAccount(student.getStudentAccount()) >0){
                return MessageVOFactory.getAddMessage(2);
        }
        return MessageVOFactory.getAddMessage(stu.addStudent(student));
    }


    //=========⑨=========


    @Autowired
    StudentService studentService;




    @Autowired
    private StudentService service;

    // 跳转到查询视图（jsp页面）
    @JWPIdentifiter("student:select")
    @RequestMapping("/forward/studentList")
    public ModelAndView forwardList(ModelAndView modelAndView) {
        modelAndView.setViewName("student/studentList");
        return modelAndView;
    }

    // 分页查询
    @ResponseBody
    @JWPIdentifiter("student:select")
    @RequestMapping("/selectList")
    public Object list(EasyuiPageVO vo) {
        if (vo.isSqlError()) {
            System.out.println("=========SqlError=========");
            return null;
        }
        String whereSql = vo.getWhereSql(_ModuleKey.Student);
        if (whereSql=="" ||"".equals(whereSql)){
            whereSql = "1=1";
        }
        return EasyuiGridData.getEasyuiGridData(service.count(whereSql),
                service.selectPage(whereSql, vo.getOrderbySQL(), vo.getPageSQL(), vo.getRows()));
    }

    //查询所有
    @ResponseBody
    @RequestMapping("/selectAll")
    public Object list() {
        return service.selectAll();
    }

    // 根据编号查询单个
    @ResponseBody
    @RequestMapping("/selectStudentById")
    public Object selectOne(String studentId) {
        if (studentId == "" || "".equals(studentId)) {
            return null;
        }
        return service.selectOne(studentId);
    }

    /**
     *  删除功能
     * @param
     * @return
     */
    @JWPIdentifiter("student:delete")
    @RequestMapping("/deleteStudent")
    @ResponseBody
    public String deleteStudent(String studentId){
        if (studentId ==  ""|| "".equals(studentId)) {
            return null;
        }
        return MessageVOFactory.getDellMessage(studentService.deleteStudent(studentId));
    }

    /**
     *  修改功能
     * @param
     * @return
     */
    @JWPIdentifiter("student:update")
    @RequestMapping("/updateStudent")
    @ResponseBody
    public String updateStudent(Student student){
        System.out.println(student.getStudentId()+"//"+student.getStudentName());
        if (student ==  null) {
            return null;
        }
        return MessageVOFactory.getUpdateMessage(studentService.updateStudent(student));
    }

    // 下载
    @JWPIdentifiter("student:select")
    @RequestMapping("/studentList/down")
    public void dow(HttpServletResponse resp ,EasyuiPageVO vo) throws ServletException, IOException {
        if (vo.isSqlError()) {
            System.out.println("======================");
            return;
        }
        List<Student> list=studentService.selectToDown(vo.getWhereSql(_ModuleKey.Student));

        JWEOfficeWebTool.downloadExcelFile(resp, list, "学生信息.xls", "学生", true);
    }

    //上传
    @JWPIdentifiter("student:insert")
    @RequestMapping("/upload")
    @ResponseBody
    public String upData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据上传的文件获得相应的对象集合
        List<Student> list=JWEOfficeWebTool.getUploadExcelFile(req, "file", Student.class);
        List<String> StudentId = new ArrayList<String>();//定义一个ID集合
        List<String> StudentAccount=new ArrayList<String>(); //定义一个账号集合
        //如果不为null则有数据
        if(list!=null) {
            //将账号编号，填充到集合当中
            for(Student student:list) {
                StudentId.add(student.getStudentId());
                StudentAccount.add(student.getStudentAccount());
            }
            //如果为null说明账号或ID不重复
            if(imp.checkExcel(StudentId).size()>0) {
                System.out.println(imp.checkExcel(StudentId).size());
                return "{\"msg\":\"学生编号已存在,请检查后上传！\"}";
            }
            if(imp.checkExcel2(StudentAccount).size()>0) {
                return "{\"msg\":\"学生账号已存在,请检查后上传！\"}";
            }
            return MessageVOFactory.getAddMessage(imp.addExcelData(list));

        }
        return "{\"msg\":\"选择的文件类型错误或文件内容为空！\"}";
    }
};




